package com.base.cn.platform.os.controller.op.advert;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.op.advert.OpAdvertService;
import com.base.cn.platform.os.service.upload.FileUploadService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 广告
 * @author ll
 * @date 2018/5/17 11:55
 */
@Controller
@RequestMapping("/admin/op/advert")
public class AdminOpAdvertController extends AdminBaseController {

    @Autowired
    private OpAdvertService opAdvertService;
    @Autowired
    private FileUploadService fileUploadService;

    /** 搜索广告列表
     * @param opAdvert 广告搜索条件
     * @param currentPage 分页
     * @return
     */
    @RequestMapping("/queryAdvertListPage")
    public String queryAdvertListPage(Model model,
                                      @RequestBody Map<String, Object> opAdvert,
                                      @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap = opAdvertService.queryAdvertListPage(opAdvert,currentPage,true,true,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/advert/ajax_advert_list");
    }

    /**
     * 搜索广告列表
     * @return
     */
    @RequestMapping("/toQueryAdvertList")
    public String toQueryAdvertList(Model model,
                                    @RequestParam(value = "placeKey",required = false,defaultValue = "")String placeKey){
        model.addAttribute("placeKey",placeKey);
        return resultPage("/op/advert/list_advert");
    }

    /**
     * 添加修改广告
     * @return
     */
    @RequestMapping("/toSaveUpdateOpAdvert")
    public String toSaveUpdateOpAdvert(Model model,
                                       @RequestParam(value = "placeKey",required = false) String placeKey,
                                       @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id){
        Map<String,Object> advert = new HashMap<>();
        if(DataUtil.idIsNotNull(id)){
            advert = opAdvertService.queryOpAdvertById(id,true,true,true);
            placeKey = advert.get("placeKey").toString();
        }
        model.addAttribute("advert",advert);
        model.addAttribute("placeKey",placeKey);
        return resultPage("/op/advert/save_advert");
    }

    /**
     * 查看广告详情
     * @return
     */
    @RequestMapping("/showAdvertInfo")
    public String showAdvertInfo(Model model,
                                 @RequestParam("id") BigDecimal id){
        if(DataUtil.idIsNotNull(id)){
            Map<String,Object>  advert = opAdvertService.queryOpAdvertById(id,true,true,true);
            model.addAttribute("advert",advert);
        }
        return resultPage("/op/advert/show_advert_info");
    }

    /**
     * 添加修改广告
     * @param opAdvert
     * @return
     */
    @RequestMapping("/saveUpdateOpAdvert")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加广告")
    public Map<String,Object> saveUpdateOpAdvert(HttpServletRequest request,
                                                 @RequestBody Map<String, Object> opAdvert){
        BigDecimal createUserId = loginUtil.getLoginUserId(request);
        opAdvert.put("createUserId",createUserId);
        return opAdvertService.saveUpdateOpAdvert(opAdvert);
    }

    /**
     * 删除广告
     * @param ids
     * @return
     */
    @RequestMapping( "/deleteAdvertByIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除广告")
    public Map<String,Object> deleteAdvertByIds(@RequestParam("ids")String ids){
        return opAdvertService.deleteAdvertByIds(ids);
    }

    /**
     * 修改广告状态
     * @return
     */
    @RequestMapping("/updateOpAdvertStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改广告状态")
    public Map<String,Object> updateOpAdvertStatus(@RequestParam("ids")String ids,
                                                   @RequestParam("status")Integer status){
        return opAdvertService.updateOpAdvertStatus(ids,status);
    }

    /**
     * 上传文件用于广告专题文件上传
     * @param file
     * @param fileParams
     * @return
     */
    @PostMapping(value = "/uploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public Map<String,Object> uploadFile(@RequestPart(value = "file") MultipartFile file,
                                         @RequestParam(value = "fileParams",required = false) String fileParams){
        return fileUploadService.advertUploadFile(file,fileParams);
    }
}
